package org.jeecg.glr.controller;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.SysConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.entity.GlrColumn;
import org.jeecg.glr.service.IGlrColumnService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/glr/glrAnalysis")
@Slf4j
public class GlrAnalysisController extends JeecgController<GlrColumn, IGlrColumnService> {

    @Autowired
    private IGlrVideoYsService glrVideoYsService;

    /**
     * 取得设备的总数量
     * @return
     */
    @PostMapping(value = "/getEquipmentTotalNumber")
    public Result<?> getEquipmentTotalNumber(){

        String orgCodesql ="";
        String userType= SysUtil.getUserInfo().getUserType();
        if(!userType.equals(SysConstant.SYS_USER_TYPE_ADMIN)&&!userType.equals(SysConstant.SYS_USER_TYPE_0)){
            orgCodesql=orgCodesql+"sys_company_code='" + SysUtil.getUserInfo().getSysCompanyCode() + "'";
        }
        String orgCodesqlWhere ="";
        String orgCodesqlAnd ="";
        if (StringUtil.isNotEmpty(orgCodesql)){
            orgCodesqlWhere =" where "+orgCodesql;
            orgCodesqlAnd =" and "+orgCodesql;
        }


        String sql1 ="select 'gateway'as a0,IFNULL(count(*),0) as a1,IFNULL(sum(if(current_work_status='Y',1,0)),0) as a2 from glr_gateway ";
        sql1=sql1+orgCodesqlWhere;
        String sql2 ="select 'light'as a0,IFNULL(count(*),0) as a1,0 as a2 from glr_light ";
        sql2=sql2+orgCodesqlWhere;
        String sql3 ="select 'led'as a0,IFNULL(count(*),0) as a1,IFNULL(sum(if(status='Y',1,0)),0) as a2 from glr_led ";
        sql3=sql3+orgCodesqlWhere;
        String sql4 ="select 'sensor'as a0,IFNULL(count(*),0) as a1,IFNULL(sum(if(status='Y',1,0)),0) as a2 from glr_sensor ";
        sql4=sql4+orgCodesqlWhere;
        String sql5 ="select 'video'as a0,IFNULL(count(*),0) as a1,IFNULL(sum(if(equipment_status='Y',1,0)),0) as a2 from glr_video ";
        sql5=sql5+orgCodesqlWhere;
        String sql6 ="select 'radio'as a0,IFNULL(count(*),0) as a1,IFNULL(sum(if(status='Idle'||status='Working',1,0)),0) as a2 from glr_radio ";
        sql6=sql6+orgCodesqlWhere;
        String sql7 ="select 'fault_alerm'as a0,IFNULL(count(*),0) as a1,0 as a2 from glr_fault_alerm where status='"+ GLRConstant.YN_N+"' ";
        sql7=sql7+orgCodesqlAnd;

        int year=DateUtils.getYear();
        String sql8 ="select 'light_data' as a0,IFNULL(sum(day_Electricity),0) as a1,IFNULL(avg(avg_power),0) as a2 from glr_light_data where date_format(create_time, '%Y')='"+year+"'";
        sql8=sql8+orgCodesqlAnd;

        String stsql=sql1+"\n union \n"+sql2+"\n union \n"+sql3+"\n union \n"+sql4+"\n union \n"+sql5+"\n union \n"+sql6+"\n union \n"+sql7+"\n union \n"+sql8;
        List<Map<String, Object>> dataList= glrVideoYsService.doGetListForJdbc(stsql);
        Float isuma10=0f;

        Float isuma11=0f;
        Float isuma12=0f;

        Float isuma21=0f;

        Float isuma31=0f;

        Float isuma41=0f;
        for(Map<String, Object> a1 :dataList){
            String sta0=a1.get("a0").toString();
            String sta1=a1.get("a1").toString();
            String sta2=a1.get("a2").toString();
            Float icounta1=Float.parseFloat(sta1);
            Float icounta2=Float.parseFloat(sta2);
            if ("gateway".equals(sta0)||"light".equals(sta0)||"led".equals(sta0)||"sensor".equals(sta0)||"video".equals(sta0)||"radio".equals(sta0)){
                isuma10=isuma10+icounta1;
                isuma11=isuma11+icounta1;
                isuma12=isuma12+icounta2;
            }
            //if ("gateway".equals(sta0)||"led".equals(sta0)||"sensor".equals(sta0)||"video".equals(sta0)||"radio".equals(sta0)){

            //}
            if ("fault_alerm".equals(sta0)){
                isuma21=isuma21+icounta1;
            }
            if ("light_data".equals(sta0)){
                isuma31=icounta1;
                isuma41=icounta2;
            }
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sum10",isuma10);  //所有的设备数
        map.put("sum11",isuma11);  //在线率用的设备总数，除了路灯的数据，现在加上路灯的数据
        map.put("sum12",isuma12);  //在线率用的在线设备数，除了路灯的数据，现在加上路灯的数据
        map.put("sum21",isuma21);  //路灯故障数量
        map.put("sum31",isuma31);  //当年总用量
        map.put("sum41",isuma41);  //当年平均功率
        return Result.ok(map);
    }


}
